package com.example.aspect;

import com.example.domain.user.vo.UserVO;
import com.example.manager.UserCacheManager;
import com.example.security.exception.ServiceImplException;
import oj.common.core.contants.Constants;
import oj.common.core.contants.JwtConstants;
import oj.common.core.enums.ResultCode;
import oj.common.core.utils.ThreadLocalUtil;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Objects;

@Component
@Aspect
public class UserStatusCheckAspect {
    //这里只需要在相应的方法上加上注解即可。
    @Autowired
    private UserCacheManager userCacheManager;
    @Before("@annotation(com.example.aspect.CheckUserStatus)")
    public void before(JoinPoint joinPoint) {
        Long userId = ThreadLocalUtil.get(JwtConstants.LOGIN_USER_ID,Long.class);
        UserVO userVO = userCacheManager.getUserVOById(userId);
        if(userId==null) {
            throw new ServiceImplException(ResultCode.FAILED_USER_NOT_EXISTS);
        }
        if(Objects.equals(userVO.getStatus(), Constants.FALSE)) {
            throw new ServiceImplException(ResultCode.FAILED_USER_BANNED);
        }
    }
}
